package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"logstore/cache"
	"logstore/model"
	"strconv"
	"time"
)

func StoreAdd(c *gin.Context) {
	store_name := c.PostForm("store_name")
	store_address := c.PostForm("store_address")
	data := model.Store{
		StoreName:    store_name,
		StoreAddress: store_address,
	}
	model.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
	})
	return
}
func OrderAdd(c *gin.Context) {
	sid, _ := strconv.Atoi(c.PostForm("sid"))
	order_num := c.PostForm("order_num")
	order_price := c.PostForm("order_price")
	order_time := c.PostForm("order_time")
	data := model.Order{
		Sid:        sid,
		OrderNum:   order_num,
		OrderPrice: order_price,
		OrderTime:  order_time,
	}
	model.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
	})
	return
}

func StoreOrder(c *gin.Context) {
	/*
		SELECT
		stores.id as StoreOrderId,
		stores.store_name as StoreName,
		stores.store_address as StoreAddress,
		orders.order_num as OrderNum,
		orders.order_price as OrderPrice,
		orders.order_time as OrderTime,
		FROM
		orders
		LEFT JOIN stores ON stores.id=orders.sid
	*/
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "2"))
	offset := (page - 1) * size

	var orders []model.StoreOrder
	redis_key := "store_order_list" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("orders").Select(
			"stores.id as StoreOrderId",
			"stores.store_name as StoreName",
			"stores.store_address as StoreAddress",
			"orders.order_num as OrderNum",
			"orders.order_price as OrderPrice",
			"orders.order_time as OrderTime",
		).Joins("LEFT JOIN stores ON stores.id=orders.sid").Offset(offset).Limit(size).Debug().Find(&orders)
		jsonData, _ := json.Marshal(orders)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &orders)
	}
	var total int64
	model.DB.Model(&model.Order{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "OK",
		"data":  orders,
		"total": total,
	})
}
func StoreOrderDetail(c *gin.Context) {
	/*
			SELECT
		stores.id as StoreOrderId,
		stores.store_name as StoreName,
		stores.store_address as StoreAddress,
		orders.order_num as OrderNum,
		orders.order_price as OrderPrice,
		orders.order_time as OrderTime
		FROM
		orders
		LEFT JOIN stores ON stores.id=orders.sid
		WHERE orders.id=1
	*/
	oid := c.Query("oid")
	var orders model.StoreOrder
	redis_key := "store_order_detail" + oid
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("orders").Select(
			"stores.id as StoreOrderId",
			"stores.store_name as StoreName",
			"stores.store_address as StoreAddress",
			"orders.order_num as OrderNum",
			"orders.order_price as OrderPrice",
			"orders.order_time as OrderTime",
		).Joins("LEFT JOIN stores ON stores.id=orders.sid").Where("orders.id=?", oid).Debug().Find(&orders)
		jsonData, _ := json.Marshal(orders)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &orders)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "OK",
		"data": orders,
	})
}
